package com.dukla.picbak.vo;

import org.apache.commons.lang3.StringUtils;

/**
 * 媒体类型枚举
 * 根据文件魔数
 */
public enum MediaType {

    /**
     * JPEG  (jpg)
     */
    JPEG("JPG", "FFD8FF"),

    /**
     * PNG
     */
    PNG("PNG", "89504E47"),

    /**
     * GIF
     */
    GIF("GIF", "47494638"),

    /**
     * TIFF (tif)
     */
    TIFF("TIF", "49492A00"),

    /**
     * Windows bitmap (bmp)
     */
    BMP("BMP", "424D"),

    BMP_16("BMP", "424D228C010000000000"), //16色位图(bmp)

    BMP_24("BMP", "424D8240090000000000"), //24位位图(bmp)

    BMP_256("BMP", "424D8E1B030000000000"), //256色位图(bmp)

    /**
     * WAVE (wav)
     */
    WAV("WAV", "57415645"),

    /**
     * AVI
     */
    AVI("AVI", "41564920"),

    /**
     * Real Audio (ram)
     */
    RAM("RAM", "2E7261FD"),

    /**
     * Real Media (rm) rmvb/rm相同
     */
    RM("RM", "2E524D46"),

    /**
     * Real Media (rm) rmvb/rm相同
     */
    RMVB("RMVB", "2E524D46000000120001"),

    /**
     * MPEG (mpg)
     */
    MPG("MPG", "000001BA"),

    /**
     * Quicktime  (mov)
     */
    MOV("MOV", "6D6F6F76"),

    /**
     * Windows Media (asf)
     */
    ASF("ASF", "3026B2758E66CF11"),

    /**
     * MIDI (mid)
     */
    MID("MID", "4D546864"),

    /**
     * MP4
     */
    MP4("MP4", "00000020667479706D70"),

    /**
     * MP3
     */
    MP3("MP3", "49443303000000002176"),

    /**
     * FLV
     */
    FLV("FLV", "464C5601050000000900"),

    /**
     * 不存在
     */
    NOT_EXITS_ENUM("NONE", "");

    private final String type;

    private final String num;

    MediaType(String type, String num) {
        this.type = type;
        this.num = num;
    }

    public String getNum() {
        return num;
    }

    public String getType() {
        return type;
    }

    /**
     * 根据类型获取魔数类型
     * @param type 类型
     * @return 结果
     */
    public static MediaType of(String type) {
        for (MediaType anEnum : values()) {
            if (anEnum.getType().equalsIgnoreCase(type)) {
                return anEnum;
            }
        }
        return MediaType.NOT_EXITS_ENUM;
    }

    /**
     * 根据十六进制流获取魔数类型
     * @param hex 十六进制流
     * @return 结果
     */
    public static MediaType byHex(String hex) {
        if (StringUtils.isBlank(hex)) {
            return MediaType.NOT_EXITS_ENUM;
        }
        for (MediaType anEnum : values()) {
            if (hex.toUpperCase().equals(anEnum.getNum())) {
                return anEnum;
            }
        }
        return MediaType.NOT_EXITS_ENUM;
    }
}
